package com.trace.learn;


/**
 * @author chenxin
 * @time 2017-03-16-11:53
 */
public class ThreadPassTest {
    private static ThreadLocal<String> threadLocal = new ThreadLocal<String>();

    private static InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<String>();

    public static void main(String[] args) {
        /***
         * threadLocal中的值无法在父子线程中传递
         */
        threadLocal.set("mainTraceId");

        Thread son = new Thread(new Runnable() {
            public void run() {
                System.out.println("son get " + threadLocal.get());
            }
        });

        son.start();

        System.out.println("main get " + threadLocal.get());

    }







    public void testInheritableThreadLocal(){
        inheritableThreadLocal.set("mainTraceIdInheritableThreadLocal");

        Thread son2 = new Thread(new Runnable() {
            public void run() {
                System.out.println(inheritableThreadLocal.get() + " son2 run");
                inheritableThreadLocal.set("son set");
                //                System.out.println(Thread.currentThread().getName());
            }
        });
        son2.start();
        try {
            son2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        /***
         * 父线程无法获取到子线程的inheritableThreadLocal的值，因为创建子线程时的ThreadLocalMap为深拷贝
         */
        System.out.println(inheritableThreadLocal.get());
        System.out.println(Thread.currentThread().getName());
        System.out.println("end");
        System.out.println(inheritableThreadLocal.get());
    }
}
